package main;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import dao.CommDAO;

import javax.swing.JTable;
import javax.swing.JTabbedPane;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;

public class Main extends JFrame {

	private CommDAO dao = new CommDAO();
	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTable table;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTable table_1;
	private JTextField textField_4;
	private JTextField textField_5;
	private JTextField textField_6;
	private JTable table_2;
	private JTextField textField_7;
	private JTextField textField_8;
	private JTable table_3;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Main frame = new Main();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Main() {
		setTitle("资产设备管理系统");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 921, 519);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		contentPane.add(tabbedPane, BorderLayout.CENTER);
		
		JPanel panel = new JPanel();
		tabbedPane.addTab("设备列表", null, panel, null);
		panel.setLayout(null);
		
		JButton btnNewButton = new JButton("查询");
		btnNewButton.setBounds(605, 19, 93, 23);
		panel.add(btnNewButton);
		
		JComboBox comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"", "未录入", "已录入"}));
		comboBox.setBounds(467, 20, 93, 21);
		panel.add(comboBox);
		
		JLabel label = new JLabel("门牌号：");
		label.setBounds(43, 23, 54, 15);
		panel.add(label);
		
		textField = new JTextField();
		textField.setBounds(96, 20, 113, 21);
		panel.add(textField);
		textField.setColumns(10);
		
		JLabel label_1 = new JLabel("存放楼层：");
		label_1.setBounds(226, 23, 54, 15);
		panel.add(label_1);
		
		textField_1 = new JTextField();
		textField_1.setBounds(270, 20, 113, 21);
		panel.add(textField_1);
		textField_1.setColumns(10);
		
		JLabel label_2 = new JLabel("设备情况：");
		label_2.setBounds(398, 23, 68, 15);
		panel.add(label_2);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(43, 73, 779, 260);
		panel.add(scrollPane);
		
		table = new JTable();
		scrollPane.setViewportView(table);
		
		JButton button = new JButton("编辑信息");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Room room = new Room();
				room.setVisible(true);
			}
		});
		button.setBounds(726, 19, 93, 23);
		panel.add(button);
		
		JButton button_1 = new JButton("录入");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				StayIn stayIn = new StayIn();
				stayIn.setVisible(true);
			}
		});
		button_1.setBounds(270, 364, 93, 23);
		panel.add(button_1);
		
		JButton btnNewButton_4 = new JButton("删除");
		btnNewButton_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CheckOut checkOut = new CheckOut();
				checkOut.setVisible(true);
			}
		});
		btnNewButton_4.setBounds(467, 364, 93, 23);
		panel.add(btnNewButton_4);
		

		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String num = "".equals(textField.getText())? "" : " and num = '" + textField.getText() + "'";
				String floor = "".equals(textField_1.getText())? "" : " and floor = '" + textField_1.getText() + "'";
				String state = "".equals(comboBox.getSelectedItem().toString())? "" : " and state = '" + comboBox.getSelectedItem().toString() + "'";
				String sql = " select * from room where 1 = 1 " + num + floor + state;
				List<Map<String, Object>> select = dao.select(sql);
				Object[] columns={"设备id","门牌号","存放楼层","设备类型", "设备价格", "当前状态"};
				Object[][] data = new Object[select.size()][columns.length];
		        for (int i = 0; i < select.size(); i++) {
		        	data[i][0] = select.get(i).get("id");
		        	data[i][1] = select.get(i).get("num");
		        	data[i][2] = select.get(i).get("floor");
		        	data[i][3] = select.get(i).get("type");
		        	data[i][4] = select.get(i).get("price");
		        	data[i][5] = select.get(i).get("state");
		        }
				System.out.println(sql);
				System.out.println(select.size());
				System.out.println(columns.length);
				DefaultTableModel dataModel = new DefaultTableModel(data, columns);
				table.setModel(dataModel);
			}
		});
		
		JPanel panel_1 = new JPanel();
		tabbedPane.addTab("设备信息列表", null, panel_1, null);
		panel_1.setLayout(null);
		
		JButton btnNewButton_1 = new JButton("查询");
		btnNewButton_1.setBounds(422, 27, 70, 23);
		panel_1.add(btnNewButton_1);
		
		JLabel label_3 = new JLabel("设备名称");
		label_3.setBounds(43, 31, 54, 15);
		panel_1.add(label_3);
		
		textField_2 = new JTextField();
		textField_2.setBounds(102, 28, 104, 21);
		panel_1.add(textField_2);
		textField_2.setColumns(10);
		
		JLabel label_4 = new JLabel("设备id：");
		label_4.setBounds(229, 31, 54, 15);
		panel_1.add(label_4);
		
		textField_3 = new JTextField();
		textField_3.setBounds(276, 28, 104, 21);
		panel_1.add(textField_3);
		textField_3.setColumns(10);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(42, 60, 718, 226);
		panel_1.add(scrollPane_1);
		
		table_1 = new JTable();
		scrollPane_1.setViewportView(table_1);
		
		JButton button_2 = new JButton("编辑信息");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Customer customer = new Customer();
				customer.setVisible(true);
			}
		});
		button_2.setBounds(523, 27, 93, 23);
		panel_1.add(button_2);

		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String idcard = "".equals(textField_2.getText()) ? "" : " and idcard = '" + textField_2.getText() + "'";
				String name = "".equals(textField_3.getText()) ? "" : " and name = '" + textField_3.getText() + "'";
				String sql = " select * from customer where 1 = 1 " + idcard + name;
				List<Map<String, Object>> select = dao.select(sql);
				
				Object[] columns={"设备id","设备名称","设备折旧","设备存放时间", "类型"};
				Object[][] data = new Object[select.size()][columns.length];
		        for (int i = 0; i < select.size(); i++) {
		        	data[i][0] = select.get(i).get("idcard");
		        	data[i][1] = select.get(i).get("name");
		        	data[i][2] = select.get(i).get("phone");
		        	data[i][3] = select.get(i).get("age");
		        	data[i][4] = select.get(i).get("sex");
		        }
				System.out.println(sql);
				System.out.println(select.size());
				System.out.println(columns.length);
				DefaultTableModel dataModel = new DefaultTableModel(data, columns);
				table_1.setModel(dataModel);
				
			}
		});
		
		JPanel panel_2 = new JPanel();
		tabbedPane.addTab("录入记录", null, panel_2, null);
		panel_2.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("身份证：");
		lblNewLabel.setBounds(27, 21, 54, 15);
		panel_2.add(lblNewLabel);
		
		textField_4 = new JTextField();
		textField_4.setBounds(76, 18, 116, 21);
		panel_2.add(textField_4);
		textField_4.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("姓名：");
		lblNewLabel_1.setBounds(202, 21, 36, 15);
		panel_2.add(lblNewLabel_1);
		
		textField_5 = new JTextField();
		textField_5.setBounds(234, 18, 116, 21);
		panel_2.add(textField_5);
		textField_5.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("设备编号：");
		lblNewLabel_2.setBounds(360, 21, 68, 15);
		panel_2.add(lblNewLabel_2);
		
		textField_6 = new JTextField();
		textField_6.setBounds(420, 18, 116, 21);
		panel_2.add(textField_6);
		textField_6.setColumns(10);
		
		JButton btnNewButton_2 = new JButton("查询");
		btnNewButton_2.setBounds(567, 17, 62, 23);
		panel_2.add(btnNewButton_2);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		scrollPane_2.setBounds(27, 49, 833, 367);
		panel_2.add(scrollPane_2);
		
		table_2 = new JTable();
		scrollPane_2.setViewportView(table_2);
		
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				String idcard = "".equals(textField_4.getText()) ? "" : " and idcard = '" + textField_4.getText() + "'";
				String name = "".equals(textField_5.getText()) ? "" : " and name = '" + textField_5.getText() + "'";
				String room_num = "".equals(textField_6.getText()) ? "" : " and room_num = '" + textField_6.getText() + "'";
				String sql = " select * from record where 1 = 1 " + idcard + name + room_num;
				List<Map<String, Object>> select = dao.select(sql);
				
				Object[] columns={"录入记录ID","登记身份证","录入人","录入房间", "录入楼层", "录入时间", "删除时间"};
				Object[][] data = new Object[select.size()][columns.length];
		        for (int i = 0; i < select.size(); i++) {
		        	data[i][0] = select.get(i).get("id");
		        	data[i][1] = select.get(i).get("idcard");
		        	data[i][2] = select.get(i).get("name");
		        	data[i][3] = select.get(i).get("room_num");
		        	data[i][4] = select.get(i).get("floor");
		        	data[i][5] = select.get(i).get("start_time");
		        	data[i][6] = select.get(i).get("end_time");
		        }
				System.out.println(sql);
				System.out.println(select.size());
				System.out.println(columns.length);
				DefaultTableModel dataModel = new DefaultTableModel(data, columns);
				table_2.setModel(dataModel);
				
			}
		});
		
		JPanel panel_3 = new JPanel();
		tabbedPane.addTab("管理员信息", null, panel_3, null);
		panel_3.setLayout(null);
		
		JLabel label_5 = new JLabel("工号：");
		label_5.setBounds(23, 10, 54, 15);
		panel_3.add(label_5);
		
		textField_7 = new JTextField();
		textField_7.setBounds(76, 7, 139, 21);
		panel_3.add(textField_7);
		textField_7.setColumns(10);
		
		JLabel label_6 = new JLabel("姓名：");
		label_6.setBounds(225, 10, 54, 15);
		panel_3.add(label_6);
		
		textField_8 = new JTextField();
		textField_8.setBounds(274, 7, 139, 21);
		panel_3.add(textField_8);
		textField_8.setColumns(10);
		
		JButton btnNewButton_3 = new JButton("查询");
		btnNewButton_3.setBounds(439, 6, 93, 23);
		panel_3.add(btnNewButton_3);
		
		JScrollPane scrollPane_3 = new JScrollPane();
		scrollPane_3.setBounds(23, 48, 843, 383);
		panel_3.add(scrollPane_3);
		
		table_3 = new JTable();
		scrollPane_3.setViewportView(table_3);
		
		JButton button_3 = new JButton("编辑信息");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Employee employee = new Employee();
				employee.setVisible(true);
			}
		});
		button_3.setBounds(568, 6, 93, 23);
		panel_3.add(button_3);
		
		btnNewButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				String id = "".equals(textField_6.getText()) ? "" : " and id = '" + textField_6.getText() + "'";
				String name = "".equals(textField_7.getText()) ? "" : " and name = '" + textField_7.getText() + "'";
				String sql = " select * from record where 1 = 1 " + id + name;
				List<Map<String, Object>> select = dao.select(sql);
				
				Object[] columns={"工号ID", "姓名","性别","年龄", "电话", "地点", "身份"};
				Object[][] data = new Object[select.size()][columns.length];
		        for (int i = 0; i < select.size(); i++) {
		        	data[i][0] = select.get(i).get("id");
		        	data[i][1] = select.get(i).get("name");
		        	data[i][2] = select.get(i).get("sex");
		        	data[i][3] = select.get(i).get("age");
		        	data[i][4] = select.get(i).get("phone");
		        	data[i][5] = select.get(i).get("place");
		        	data[i][6] = select.get(i).get("role");
		        }
				System.out.println(sql);
				System.out.println(select.size());
				System.out.println(columns.length);
				DefaultTableModel dataModel = new DefaultTableModel(data, columns);
				table_3.setModel(dataModel);
				
			}
		});
	}
}
